package com.sharesrc.jdev.wiimotedemo;

import java.util.Vector;

import wiiusej.values.Orientation;
import wiiusej.values.RawAcceleration;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;

import com.sharesrc.jdev.wiimotedemo.listener.IAccelerationListener;
import com.sharesrc.jdev.wiimotedemo.listener.IButtonListener;
import com.sharesrc.jdev.wiimotedemo.listener.IRotationListener;

public enum WiiUseJToWiigeeEventsDispatcher {

	INSTANCE;

	Vector<IButtonListener> wiigeeButtonListeners = new Vector<IButtonListener>(1);
	Vector<IAccelerationListener> wiigeeAccelerationListeners = new Vector<IAccelerationListener>(1);
	Vector<IRotationListener> wiigeeRotationListeners = new Vector<IRotationListener>(1);

	private WiiUseJToWiigeeEventsDispatcher() {

	}

	public boolean addWiigeeButtonListener(IButtonListener wiigeeButtonListener) {
		if (!this.wiigeeButtonListeners.contains(wiigeeButtonListener)) {
			return this.wiigeeButtonListeners.add(wiigeeButtonListener);
		}
		return false;
	}

	public boolean addWiigeeAccelerationListener(IAccelerationListener wiigeeAccelerationListener) {
		if (!this.wiigeeAccelerationListeners.contains(wiigeeAccelerationListener)) {
			return this.wiigeeAccelerationListeners.add(wiigeeAccelerationListener);
		}
		return false;
	}

	public boolean addWiigeeRotationListener(IRotationListener wiigeeRotationListener) {
		if (!this.wiigeeRotationListeners.contains(wiigeeRotationListener)) {
			return this.wiigeeRotationListeners.add(wiigeeRotationListener);
		}
		return false;
	}

	public void dispatchButtonPressEvent(WiimoteButtonsEvent evt) {
		int button = evt.getButtonsJustPressed();
		for (IButtonListener listener : this.wiigeeButtonListeners) {
			listener.buttonPressReceived(button);
		}
	}

	public void dispatchButtonReleaseEvent(WiimoteButtonsEvent evt) {
		int button = evt.getButtonsJustReleased();
		for (IButtonListener listener : this.wiigeeButtonListeners) {
			listener.buttonReleaseReceived(button);
		}
	}

	public void dispatchAccelerationEvent(MotionSensingEvent evt) {
		RawAcceleration rawAcceleration = evt.getRawAcceleration();
		double[] data = { rawAcceleration.getX(), rawAcceleration.getY(), rawAcceleration.getZ() };
		for (IAccelerationListener listener : this.wiigeeAccelerationListeners) {
			listener.accelerationReceived(data);
		}
	}

	public void dispatchRotationEvent(MotionSensingEvent evt) {
		Orientation orientation = evt.getOrientation();
		for (IRotationListener listener : this.wiigeeRotationListeners) {
			listener.rotationReceived(orientation.getPitch(), orientation.getRoll(), orientation.getYaw());
		}
	}
}
